package com.ccp.dev.form.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.form.dao.SysBusEventDao;
import com.ccp.dev.form.model.SysBusEvent;
import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 对象功能:sys_bus_event Service类
 * 开发人员:chenjing
 * 创建时间:2019-07-09
 */
@Service
public class SysBusEventService extends BaseService<SysBusEvent>
{
	@Resource
	private SysBusEventDao sysBusEventDao;

	/**
	 * 根据json字符串获取SysBusEvent对象
	 * @param json
	 * @return
	 */
	public SysBusEvent getSysBusEvent(String json){
		JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[] { "yyyy-MM-dd" }));
		if(StringUtil.isEmpty(json)){
			return null;}
		JSONObject obj = JSONObject.fromObject(json);
		SysBusEvent sysBusEvent = (SysBusEvent) JSONObject.toBean(obj, SysBusEvent.class);
		return sysBusEvent;
	}

	/**
	 * 保存 sys_bus_event 信息
	 * @param sysBusEvent
	 */
	@Transactional(rollbackFor = Exception.class)
	public void save(SysBusEvent sysBusEvent){
		String id=sysBusEvent.getId();
		if(StringUtil.isEmpty(id)){
			id= UUIDUtils.getUUIDFor32();
			sysBusEvent.setId(id);
			this.add(sysBusEvent);
		}
		else{
			this.update(sysBusEvent);
		}
	}

	/**
	 * 根据formKey获取脚本验证。
	 * @param formKey
	 * @return
	 */
	public SysBusEvent getByFormKey(String formKey){
		return sysBusEventDao.getByFormKey(formKey);
	}


}
